package com.iwhalecloud.bss.kite.cucc.common.constant;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public final class DES3Util {

    public static final String CODE = "UTF-8";

    private static String ivParam = "12345678";

    private static String keyParam = "IWHALECLOUD_GX_BSS30_DES"; // 加密密钥，长度为24字节

    private DES3Util() {
    } 

    public static void main(String[] args) throws Exception {
        
        DES3Util DES3Util = new DES3Util();
        
        String date = "112245";
        // 加密数据
        String tridesResult = DES3Util.encryptForDES3(date);
        // System.out.println(date + ">>>3DES 加密>>>" + tridesResult);
        // 解密数据
        String tridesPlain = DES3Util.decryptForDES3(tridesResult);
        // System.out.println(date + ">>>3DES 解密>>>" + tridesPlain);
    }

    /*
     * 3DES 加密
     */
    public static String encryptForDES3(String data) throws Exception {
        Base64 base64 = new Base64();
        SecretKeySpec secretKey = new SecretKeySpec(keyParam.getBytes(), "DESede");
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(ivParam.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        byte[] cipherBytes = cipher.doFinal(data.getBytes());

        String result = base64.encodeToString(cipherBytes);
        return result;
    }

    /*
     * 3DES 解密
     */
        public static String decryptForDES3(String data) throws Exception {
        Base64 base64 = new Base64();
        byte[] encrypted = base64.decode(data); // 先用base64解密

        SecretKey secretKey = new SecretKeySpec(keyParam.getBytes(), "DESede");
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(ivParam.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        byte[] original = cipher.doFinal(encrypted);
        String originalString = new String(original);
        return originalString;
    }

    /*
     * 3DES 加密
     */
    public static byte[] encrypt(byte[] data) throws Exception {
        SecretKey secretKey = new SecretKeySpec(keyParam.getBytes(), "DESede");
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(ivParam.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        byte[] cipherBytes = cipher.doFinal(data);
        return cipherBytes;
    }

    /*
     * 3DES 解密
     */
    public static byte[] decrypt(byte[] data) throws Exception {
        SecretKey secretKey = new SecretKeySpec(keyParam.getBytes(), "DESede");
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(ivParam.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        byte[] plainBytes = cipher.doFinal(data);
        return plainBytes;
    }

}
